<?php
/**'
 * 地址管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/3
 * Time: 9:45
 */
class Controller_Wwwmall2_Address extends Controller_Wwwmall2_Abstract {

    public function listAction(){
        global $_F;
        $dataList = Service_Address::getAddressList($_F['uid']);
        //echo json_encode($dataList);exit;
        $this->assign('dataList',$dataList);
        $refer = FRequest::getString('refer');
        if($refer == '/order/submit'){
            Service_VisitLog::add($_F['uid'],5);
        }
        $this->assign('refer',$refer);
        $this->display('address/list');
    }

    /**
     * 重置默认地址
     */
    public function resetDefaultAction(){
        global $_F;
        $id = FRequest::getPostInt('id');
        Service_Address::resetDefaultAddressId($id,$_F['uid']);
        FResponse::output(array('status'=>0,'msg'=>'重置成功'));
    }

    /**
     * 编辑收货地址
     */
    public function saveAction(){
        global $_F;
        $refer = FRequest::getString('refer');
        if($this->isPost()){
            $this->saveAddress();
        }
        $id = FRequest::getInt('id');

        if($id > 0){
            $address = Service_Address::getAddress($id);
            $_GET = array_merge($address,$_GET);
            $this->assign('id',$id);
        }else{
            if($refer == '/order/submit'){
                Service_VisitLog::add($_F['uid'],6);
            }
        }

        $this->assign('refer',$refer);
        $this->display('address/save');
    }

    /**
     * 百度地图定位
     */
    public function baiduMapAction(){
        global $_F;
        $refer = FRequest::getString('refer');
        if($refer == '/order/submit'){
            Service_VisitLog::add($_F['uid'],7);
        }
        require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
        $options = array(
            'token' => FConfig::get('wechat.token'),
            'appid' => FConfig::get('wechat.appid'),
            'appsecret' => FConfig::get('wechat.appsecret'),
        );
        $weixin = new WeixinChat($options);
        $data = $weixin->getJsSDKConfig($this->getFullUrl());
        $this->assign('data',$data);
        $this->display('address/baiduMap');

    }

    private function saveAddress(){
        global $_F;
        $id = FRequest::getPostInt('id');
        $contact = FRequest::getPostString('contact');
        $phone = FRequest::getPostString('phone');
        $province = FRequest::getPostString('province');
        $city = FRequest::getPostString('city');
        $county = FRequest::getPostString('county');
        $address = FRequest::getPostString('address');
        $housemember = FRequest::getPostString('housemember');
        $sex = FRequest::getPostInt('sex');
        $lat = FRequest::getPostString('lat');
        $lng = FRequest::getPostString('lng');
        $refer = FRequest::getPostString('refer');
        if($id > 0){
            $addr = Service_Address::getAddress($id);
            $is_default = $addr['is_default'];
            $msg = '更新成功';
            if($refer){
                $refer = '/address/list?refer=' . $refer;
            }else{
                $refer = '/address/list';
            }
        }else{
            $refer = !empty($refer)?$refer:'/address/list';
            $is_default = 1;
            $msg = '添加成功';
        }
            $refer = !empty($refer)?$refer:'/address/list';

        $result = Service_Address::saveAddress($id,$_F['uid'],$contact,$phone,$city,$province,$county,$address,$housemember,$sex,$lat,$lng,$is_default);
        if($result)
            FResponse::output(array('status'=>0,'msg'=>$msg,'url'=>$refer));
        else
            FResponse::output(array('status'=>1,'msg'=>'最多添加10条地址'));
        exit;
    }

    public function delAction(){
        global $_F;
        $id = FRequest::getPostInt('id');
        Service_Address::delAddress($_F['uid'],$id);
        FResponse::output(array('status'=>0,'msg'=>'删除成功'));
    }
}